<?php

session_start();
require_once ('common.php');
if (!$user['is_guest'])
{
    header('location: ' . APP_URL);
    exit;
}
$username = trim(htmlspecialchars($_POST['username']));
$password = trim(htmlspecialchars($_POST['password']));
$c_password = trim(htmlspecialchars($_POST['c-password']));
$email = trim(htmlspecialchars($_POST['email']));
$captcha = trim(htmlspecialchars($_POST['captcha']));
if (!empty($username) && !empty($password) && !empty($c_password) && !empty($email) && !empty($captcha))
{
    $pass_encode = sha1(md5($password));
    $check = $db->query('SELECT * FROM bbs_members WHERE username = %1', array($username));
    if (!empty($check))
        $error = 'ชื่อผู้ใช้นี้ซ้ำ กรุณาใช้ชื่ออื่น';
    else
    {
        if ($_SESSION['iForumCaptcha']['result'] == $captcha)
        {
            $reg_data = array('username' => $username, 'realname' => $username, 'password' => $pass_encode, 'email' => $email);
            $db->insertRow('bbs_members', $reg_data);
            $message = 'สมัครสมาชิกเรียบร้อยแล้ว <a href="' . APP_URL . '/login.php">เข้าสู่ระบบ</a>';
        }
        else
        {
            $error = 'บวกเลขไม่ถูกต้อง ลองคิดดูใหม่อีกครั้ง';
        }
    }
}
else
{
    $capchar = new Captcha();
    $capchar->session = "iForumCaptcha";
    $captext = $capchar->display();
}
$pagetitle = 'สมัครสมาชิก';
$smarty->assign('pageid', 'signup');
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('header.html');
$smarty->assign('error', $error);
$smarty->assign('message', $message);
$smarty->assign('captcha', $captext);
$smarty->display('register.html');
require_once ('sidebar.php');
require_once ('footer.php');

?>